﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CM_GetContactFeeBySellmanID]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_CM_GetContactFeeBySellmanID];
GO
CREATE PROCEDURE [dbo].[sproc_CM_GetContactFeeBySellmanID]
@sellmanid int,
@begintime datetime,
@endtime datetime

AS
    if(@sellmanid<>0)
    begin
        SELECT UDS_CM_ClientContact.ClientID,SUM(FEE) AS Fee FROM UDS_CM_ClientContact
        WHERE MarketmanID=@sellmanid 
        AND ContactTime between @begintime and dateadd(day,1,@endtime)
        GROUP BY UDS_CM_ClientContact.ClientID
    end
    else
    begin
        SELECT UDS_CM_ClientContact.ClientID,SUM(FEE) AS Fee FROM UDS_CM_ClientContact
        WHERE 
        ContactTime between @begintime and dateadd(day,1,@endtime)
        GROUP BY UDS_CM_ClientContact.ClientID
    end